﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ProjectSem3
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        private dataDataContext dc = new dataDataContext();
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!Page.IsPostBack)
            {
                int productid=Int32.Parse(Request.QueryString["id"]);
                Product pro=this.dc.Products.Single(p=>p.ProductID==productid);

                var imgpro = this.dc.ImagesPros.Where(i=>i.ProductID == pro.ProductID).Skip(0).Take(1);

                foreach (var i in imgpro)
                {
                    this.BigImage.HRef = "~/images/Product/" + i.NameImg;
                    this.SmallImage.Src = "~/images/Product/" + i.NameImg;
                }

                this.NameProduct.Text = pro.Name;
                this.HyperLink2.Text = pro.Name;
                this.ProductID.Text=pro.ProductID.ToString();

                Supplier sup = this.dc.Suppliers.Single(s=>s.SupplierID== pro.SupplierID);
                this.Suppiler.Text=sup.Name;

                Category cat = this.dc.Categories.Single(c=>c.CategoryID == pro.CategoryID);
                this.category.Text = cat.Name;

                this.Warranty.Text = pro.Warranty+" Months";
                this.Status.Text = pro.Status.ToString();
                this.DateUpdate.Text = pro.DateUpdate.ToString();
                this.view(pro.ProductID);
                this.Description.Text = pro.Description;

                this.Repeater_Product.DataSource = this.dc.Products.ToList();
                this.Repeater_Product.DataBind();

                this.RepeaterComment.DataSource = this.dc.Comments.Where(c=>c.ProductID== pro.ProductID).ToList();
                this.RepeaterComment.DataBind();
            }
        }

        protected void Repeater_Product_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            HiddenField productID = e.Item.FindControl("productID") as HiddenField;
            var img = this.dc.ImagesPros.Where(i => i.ProductID == Int32.Parse(productID.Value)).Skip(0).Take(1);
            Image image = e.Item.FindControl("image") as Image;
            foreach (var a in img)
            {
                image.ImageUrl = "~/images/Product/" + a.NameImg;
            }
        }
        protected void OrderNow_Click(object sender, EventArgs e)
        {
            if (Session["cart"] == null)
            {
                List<Item> cart = new List<Item>();
                Product pro = this.dc.Products.Single(p => p.ProductID == Int32.Parse(this.ProductID.Text));
                cart.Add(new Item(pro, 1));
                Session["cart"] = cart;
            }
            else
            {
                List<Item> cart = (List<Item>)Session["cart"];
                int index = this.IndexOfItem(Int32.Parse(this.ProductID.Text));
                if (index != -1)
                {
                    cart[index].Quantity++;
                }
                else
                {
                    Product pro = this.dc.Products.Single(p => p.ProductID == Int32.Parse(this.ProductID.Text));
                    cart.Add(new Item(pro, 1));                    
                }
                Session["cart"] = cart;
            }
            Response.Redirect("Cart.aspx");
        }
        private int IndexOfItem(int productID)
        {
            List<Item> cart = (List<Item>)Session["cart"];
            for (int i = 0; i < cart.Count; i++)
            {
                if (cart[i].Pro.ProductID == productID)
                    return i;
            }
            return -1;
        }

        protected void BtSend_Click(object sender, EventArgs e)
        {
            if (this.TxtName.Text != "" && this.TxtEmail.Text != "" && this.TxtContent.Text != "")
            {
                Comment cm = new Comment();
                cm.ProductID = Int32.Parse(this.ProductID.Text);
                cm.Name = this.TxtName.Text;
                cm.Email = this.TxtEmail.Text;
                cm.Content = this.TxtContent.Text;

                this.dc.Comments.InsertOnSubmit(cm);
                this.dc.SubmitChanges();

                Response.Redirect("Detail.aspx?id="+this.ProductID.Text);
            }
        }
        private void view(int product)
        {
            Product pro = this.dc.Products.Single(p=>p.ProductID==product);
            int views = (int)pro.View + 1;

            pro.View = views;
            this.dc.SubmitChanges();
            this.Views.Text = views.ToString();
        }
    }
}